package com.sailfish.springbootdemo.dao.db7;


import com.sailfish.springbootdemo.common.DB7TableName;
import com.sailfish.springbootdemo.pojo.db7.TbKnowledgeBaseFile;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TbKnowledgeBaseFileDao {

    @Select("<script>" +
            "select " +
            "* " +
            "from " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "where flow_no = #{flowNo} and flow_level = #{flowLevel}" +
            "</script>"
    )
    List<TbKnowledgeBaseFile> getTbKnowledgeBaseFileByFlowNo(@Param("flowNo") String flowNo,
                                                             @Param("flowLevel") Integer flowLevel);

    @Select("<script>" +
            "SELECT * FROM " +DB7TableName.TB_KNOWLEDGE_BASE_FILE +
            " <where>" +
            "  AND param_Key IN " +
            "<foreach item='item' index='index' collection='paramKeys' open='(' separator=',' close=')'>" +
            "       #{item}" +
            "   </foreach>" +
            "</where>" +
            "</script>")
    List<TbKnowledgeBaseFile> getTbKnowledgeBaseFileByParams(@Param("paramKeys") List<String> paramKeys);

    @Select("<script>" +
            "select " +
            "* " +
            "from " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "where file_id = #{id}" +
            "</script>"
    )
    TbKnowledgeBaseFile getTbKnowledgeBaseFileById(@Param("id") Integer id);

    @Select("<script>" +
            "select " +
            "* " +
            "from " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "where secret_key = #{secret_key}" +
            "order by file_id desc "+
            "limit 1 "+
            "</script>"
    )
    TbKnowledgeBaseFile getTbKnowledgeBaseFileBySecretkey(@Param("secret_key") String secret_key);

    @Select("<script>" +
            "select " +
            "* " +
            "from " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "where param_key = #{param_key}" +
            "</script>"
    )
    List<TbKnowledgeBaseFile> getTbKnowledgeBaseFileByParamKey(@Param("param_key") String param_key);

    @Insert("<script>" +
            "insert into " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "( "+
            "flow_no, " +
            "flow_level, " +
            "create_time, " +
            "create_user, " +
            "param_key,"+
            "file_name, " +
            "file_extend, " +
            "file_relate_path, " +
            "file_type, " +
            "url, " +
            "file_status, " +
            "remark, " +
            "status, " +
            "sort, " +
            "secret_key, " +
            "file_sid " +
            ")"+
            "values "+
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(" +
            "#{entity.flow_no}, " +
            "#{entity.flow_level}, " +
            "#{entity.create_time}, " +
            "#{entity.create_user}, " +
            "#{entity.param_key}," +
            "#{entity.file_name}, " +
            "#{entity.file_extend}, " +
            "#{entity.file_relate_path}, " +
            "#{entity.file_type}, " +
            "#{entity.url}, " +
            "#{entity.file_status}, " +
            "#{entity.remark}, " +
            "#{entity.status}, " +
            "#{entity.sort}, " +
            "#{entity.secret_key}, " +
            "#{entity.file_sid} " +
            ")"+
            "</foreach>"+
            "</script>"
    )
    Integer insertTbKnowledgeBaseFileBatch(@Param("entities") List<TbKnowledgeBaseFile> entities);

    @Update("<script>" +
            "update " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "set " +
            " <if test='entity.file_name != null'>file_name = #{entity.file_name},</if>" +
            " <if test='entity.file_extend != null'>file_extend = #{entity.file_extend},</if>" +
            " <if test='entity.file_relate_path != null'>file_relate_path = #{entity.file_relate_path},</if>" +
            " <if test='entity.file_type != null'>file_type = #{entity.file_type},</if>" +
            " <if test='entity.url != null'>url = #{entity.url},</if>" +
            " <if test='entity.file_status != null'>file_status = #{entity.file_status},</if>" +
            " <if test='entity.remark != null'>remark = #{entity.remark},</if>" +
            " <if test='entity.status != null'>status = #{entity.status},</if>" +
            " <if test='entity.sort != null'>sort = #{entity.sort},</if>" +
            " <if test='entity.secret_key != null'>secret_key = #{entity.secret_key},</if>" +
            " <if test='entity.file_sid != null'>file_sid = #{entity.file_sid},</if>" +
            " update_time = #{entity.update_time}," +
            " update_user = #{entity.update_user}" +
            " where id = #{entity.id}" +
            "</script>"
    )
    Integer updateTbKnowledgeBaseFile(@Param("entity") TbKnowledgeBaseFile entity);

    @Delete("<script>" +
            "delete from " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "where flow_no = #{flowNo} and flow_level = #{flowLevel}" +
            "</script>"
    )
    Integer deleteTbKnowledgeBaseFile(@Param("flowNo") String flowNo, @Param("flowLevel") Integer flowLevel);

    @Delete("<script>" +
            "delete from " + DB7TableName.TB_KNOWLEDGE_BASE_FILE + " " +
            "where flow_no = #{flowNo}" +
            "</script>"
    )
    Integer deleteTbKnowledgeBaseFileByFlowNo(@Param("flowNo") String flowNo);
}
